package poli.tidia.mobile;

import java.util.List;

import poli.tidia.mobile.model.Folder;
import poli.tidia.mobile.model.ForumThread;
import poli.tidia.mobile.model.Message;
import poli.tidia.mobile.model.Resource;
import poli.tidia.mobile.model.Site;
import poli.tidia.mobile.proxy.ForunsProxy;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MessagesListActivity extends ListActivity implements OnClickListener{

	@Override
	public void onCreate(Bundle saved) {
		super.onCreate(saved);

		Bundle bundle = getIntent().getExtras();

		if (bundle != null) {
			Site selectedSite = (Site) bundle.getSerializable("selectedSite");
			ForumThread selectedThread = (ForumThread) bundle.getSerializable("selectedThread");
			ForunsProxy forumProxy = new ForunsProxy();
			List<Message> messages = forumProxy.getThreadMessages(selectedSite, selectedThread);

			getListView().setBackgroundColor(Color.WHITE);

			MessageArrayAdapter adapter = new MessageArrayAdapter(this, messages, this);
			setListAdapter(adapter);
		}
	}

	@Override
	public void onClick(View v) {
		try {
			Bundle b = getIntent().getExtras();
			if (b != null) {
	
				Site selectedSite = (Site) b.getSerializable("selectedSite");			
	
				Intent selectedResourceIntent = new Intent(MessagesListActivity.this,
						MessageReplyActivity.class);
				selectedResourceIntent.putExtra("selectedSite", selectedSite);
				selectedResourceIntent.putExtra("selectedMessage", (Message) getListAdapter().getItem((Integer)v.getTag()));
	
				startActivity(selectedResourceIntent);
			}
		}
		catch (Exception e) {
			Log.w("teste", e);
		}
	}
	
}
